package ShenBlueCenter.Inc;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ShenBlueInc.Center.Login_Center;
import ShenBlueInc.Center.User_Center;
import ShenBlueTool.isString;

/**
 * Servlet implementation class ModifyUser
 */
@WebServlet("/imp/ModifyUser")
public class ModifyUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		response.setCharacterEncoding("utf8");
		int Mode=Integer.parseInt(request.getParameter("mode"));//1完善用户信息 2修改密码
		HttpSession session=request.getSession();
		String user_id=(String) session.getAttribute("User_id");
		isString isString=new isString();
		User_Center userC=new User_Center();
		if(Mode==1) {
			String User_name=request.getParameter("name");
			String User_tel=request.getParameter("tel");
			String User_email=request.getParameter("email");
			String User_fullname=request.getParameter("fullname");
			
			if(isString.IsEmpty(User_name,User_tel,User_email,User_fullname)) {
				response.getWriter().write("{\"error\":\"禁止留空\"}");
				return;
			}
			System.out.println(session.getAttribute("User_telephone"));
			if(!session.getAttribute("User_telephone").equals(User_tel)) {
				Login_Center loginc=new Login_Center();
				if(loginc.SelectTele(User_tel)) {
					response.getWriter().write("{\"error\":\"您的手机号已经注册过了\"}");
					return;
				}
			}
			
			if(userC.ModifyUser(user_id, User_name, User_tel, User_email, User_fullname)) {
				response.getWriter().write("{\"error\":\"修改失败具体原因请咨询管理员\"}");
				return;
			}
			session.invalidate();
			response.getWriter().write("{\"msg\":\"修改成功\"}");
			return;
		}
		if(Mode==2) {
			String NewPwd=request.getParameter("NewPwd");
			String OldPwd=request.getParameter("OldPwd");
			if(isString.IsEmpty(NewPwd,OldPwd)) {
				response.getWriter().write("{\"error\":\"禁止留空\"}");
				return;
			}
			if(!session.getAttribute("User_Password").equals(OldPwd)) {
				response.getWriter().write("{\"error\":\"原密码输入错误，请重新输入\"}");
				return;
			}
			if(userC.ModifyPassWord(user_id, NewPwd)) {
				response.getWriter().write("{\"error\":\"密码修改失败请重新修改\"}");
				return;
			}
			session.invalidate();
			response.getWriter().write("{\"msg\":\"密码修改成功\"}");
			return;
		}
	}

}
